document.addEventListener('click', (e) => {
  if (e.target.closest('.delCom')) {
    let li = e.target.parentNode.parentNode.parentNode
    let cid = li.dataset.cid
    $.ajax({
      url: "../control/delCom.php",
      type: 'post',
      data: { cid },
      success: (res) => {
        if (res.code == 200) {
          li.remove()
          document.querySelector('.box-reply span:nth-child(1) span').innerHTML -= res.num
          saveMsg(res.msg)
        } else {
          // console.log(res.msg)
          errMsg(res.msg)
        }
      }, error: (err) => {
        console.log(err)
      }
    })
  }
  if (e.target.closest('.delRep')) {
    let li = e.target.parentNode.parentNode.parentNode
    let ul = li.parentNode
    let rid = li.dataset.rid
    console.log(li)
    console.log(rid)
    $.ajax({
      url: "../control/delRep.php",
      type: 'post',
      data: { rid },
      success: (res) => {
        if (res.code == 200) {
          li.remove()
          document.querySelector('.box-reply span:nth-child(1) span').innerHTML -= res.num
          saveMsg(res.msg)
          console.log(ul.querySelectorAll('li').length)
          if (ul.querySelectorAll('li').length == 0) {
            ul.parentNode.querySelector('.user-reply-footer .shou').remove()
          }
        } else {
          // console.log(res.msg)
          errMsg(res.msg)
        }
      }, error: (err) => {
        console.log(err)
      }
    })
  }
})

function errMsg(msg) {
  let dzErr = document.querySelector('.dzErr')
  dzErr.querySelector('span').textContent = msg
  dzErr.style.transform = 'translateY(0)'
  setTimeout(() => {
    dzErr.style.transform = 'translateY(-100px)'
  }, 3000)
}
function saveMsg(msg) {
  let saveMsg = document.querySelector('.saveMsg')
  saveMsg.querySelector('span').textContent = msg
  saveMsg.style.transform = 'translateY(0)'
  setTimeout(() => {
    saveMsg.style.transform = 'translateY(-100px)'
  }, 2000)
}